package com.hzpd.hainan.module.usercenter.acticity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.model.event.FontSizeEvent;
import com.hzpd.hainan.model.version.VersionBean;
import com.hzpd.hainan.model.version.VersionEntity;
import com.hzpd.hainan.module.version.VersionCheck;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.TUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SystemSetActivity extends ToolBarActivity {
//    public static final int textSize_normal = 19;
//    public static final int textSize_big = 22;
//    public static final int textSize_small = 16;
    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.tv_little_activity_systemset)
    TextView setTextView_litle;
    @BindView(R.id.tv_normal_activity_systemset)
    TextView setTextView_normal;
    @BindView(R.id.tv_big_activity_systemset)
    TextView setTextView_big;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.rl_version)
    RelativeLayout rlVersion;
    @BindView(R.id.ziti_id)
    TextView zitiId;
    @BindView(R.id.yijian_id)
    TextView yijianId;
    @BindView(R.id.jiancha_id)
    TextView jianchaId;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tvVersion.setText(getVersionName());
        int testsize = spu.getTextSize();
        clearAllTextColor();
        switch (testsize) {
            case CODE.textSize_small:
                setTextView_litle.setTextColor(getResources().getColor(R.color.cyol_bg));
                break;
            case CODE.textSize_normal:
                setTextView_normal.setTextColor(getResources().getColor(R.color.cyol_bg));
                break;
            case CODE.textSize_big:
                setTextView_big.setTextColor(getResources().getColor(R.color.cyol_bg));
                break;
        }

//        spu.setTextSize(16);
//        postEvent(16);
    }

    @Override
    public void initView() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            tv_title.setText("系统设置");
            zitiId.setText(getResources().getString(R.string.zitidaxiao));
            yijianId.setText(getResources().getString(R.string.jianyifenkui));
            jianchaId.setText(getResources().getString(R.string.jianchagengxin));
            setTextView_litle.setText(getResources().getString(R.string.xiao));
            setTextView_normal.setText(getResources().getString(R.string.zhong));
            setTextView_big.setText(getResources().getString(R.string.da));
        } else {
            tv_title.setText("མ་ལག་སྒྲིག་བཀོད།");
            zitiId.setText(getResources().getString(R.string.zitidaxiao_zang));
            yijianId.setText(getResources().getString(R.string.jianyifenkui_zang));
            jianchaId.setText(getResources().getString(R.string.jianchagengxin_zang));
            setTextView_litle.setText(getResources().getString(R.string.xiao_zang));
            setTextView_normal.setText(getResources().getString(R.string.zhong_zang));
            setTextView_big.setText(getResources().getString(R.string.da_zang));
        }
    }

    @Override
    public String initToolBarTitle() {
        return null;
    }

    @Override
    public int setMyContentView() {
        return R.layout.activity_system_set;
    }

    @OnClick({R.id.user_suggest_system_set, R.id.rl_version})
    public void Suggest(View view) {
        switch (view.getId()) {
            case R.id.rl_version:
                checkUpdata();
                break;
            case R.id.user_suggest_system_set:
                Intent intent = new Intent(mContext, UserSuggestActivity.class);
                startActivity(intent);
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @OnClick({R.id.tv_little_activity_systemset, R.id.tv_normal_activity_systemset,
            R.id.tv_big_activity_systemset})
    public void fontChangClick(View view) {
        clearAllTextColor();
        switch (view.getId()) {
            case R.id.tv_little_activity_systemset:
                setTextView_litle.setTextColor(getResources().getColor(
                        R.color.cyol_bg));
                spu.setTextSize(CODE.textSize_small);
//                postEvent(textSize_small);
                break;
            case R.id.tv_normal_activity_systemset:
                setTextView_normal.setTextColor(getResources().getColor(
                        R.color.cyol_bg));
                spu.setTextSize(CODE.textSize_normal);
//                postEvent(textSize_normal);
                break;
            case R.id.tv_big_activity_systemset:
                setTextView_big.setTextColor(getResources().getColor(
                        R.color.cyol_bg));
                spu.setTextSize(CODE.textSize_big);
//                postEvent(textSize_big);
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void clearAllTextColor() {
        setTextView_litle.setTextColor(getResources().getColor(
                R.color.title_color));
        setTextView_normal.setTextColor(getResources().getColor(
                R.color.title_color));
        setTextView_big.setTextColor(getResources().getColor(R.color.title_color));
    }


    private void postEvent(int textSize) {
        FontSizeEvent event = new FontSizeEvent();
        event.setFontSize(textSize);
        EventBus.getDefault().post(event);

    }

    /*
   * 获取当前程序的版本号
   */
    private String getVersionName() {
        try {
            // 获取PackageManager的实例
            PackageManager packageManager = getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
            return "V" + packInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "V1.0";
    }

    //检测更新
    private void checkUpdata() {
        Map<String, String> map = new HashMap<>();
        Factory.provideHttpService().checkUpdata()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<VersionEntity>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(VersionEntity entity) {
                        if (entity != null && "200".equals(entity.code)) {
                            updata(entity.data);
                        } else {
                            TUtils.toast(entity.msg);
                        }


                    }
                });
    }

    private void updata(VersionBean bean) {
        VersionCheck mVersionCheck = new VersionCheck(this, bean, null);
        mVersionCheck.beginCheckVersion();
    }
}
